/*******************************************************************************
 * Copyright (c) Jerges A. Bello G. 2014. Reservado todos los derechos.
 ******************************************************************************/
package com.jabex.persistence.dao;

import com.jabex.persistence.model.Identity;
import org.hibernate.HibernateException;

import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 * Created by Jerges Bello on 14/05/2014.
 */
public abstract class AbstractDAO<B extends Identity> implements IAccessDAO<B> {

	protected Class<B> modelClass;
	private SpringHibernateDAO<B> accessDao;


	@SuppressWarnings("unchecked")
	private void init() {
		ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
		modelClass = (Class<B>) parameterizedType.getActualTypeArguments()[0];
	}


	protected AbstractDAO() {
		init();
		accessDao = new SpringHibernateDAO<B>(modelClass);
	}

	@Override
	public String persist(B entity) throws HibernateException {
		return accessDao.persist(entity);
	}

	@Override
	public boolean update(B entity) throws HibernateException {
		return accessDao.update(entity);
	}

	@Override
	public boolean delete(B entity) throws HibernateException {
		return accessDao.delete(entity);
	}

	@Override
	public B getObjectById(String id) throws HibernateException {
		return accessDao.getObjectById(id);
	}

	@Override
	public List<B> getObjectByParameters(B bean) throws HibernateException {
		return accessDao.getObjectByParameters(bean);
	}

	@Override
	public List<B> getAllRows(B entity) throws HibernateException {
		return accessDao.getAllRows(entity);
	}


}
